<template>
  <div class="elm-edit one_road">
    <elm-page-bb class="ei_edit" v-loading="loading">

      <el-form ref="model" :model="model" size="small" label-width="120px">
        <elm-row>
          <el-form-item label="任务内容" prop="content" :rules="[{ required: true, message: '内容 必须填写'}]">
            <el-input :disabled="!is_creater" type="textarea" :rows="2" v-model="model.content"></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="接收人" prop="to_person" :rules="[{ required: true, message: '接收人 必选'}]">
            <elm-ei :disabled="edit_type!='create'" v-model="model.to_person" :input-add="false"></elm-ei>
          </el-form-item>
          <el-form-item label="重要程度" prop="zycd" >
            <el-select v-model="model.zycd" size="mini" style="width:100%;" :disabled="!is_creater">
              <el-option v-for="item in zycdList"
                         :key="item.name" :label="item.name" :value="item.name">
              </el-option>
            </el-select>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="状态" prop="status" :rules="[{ required: true, message: '状态 必选'}]">
            <el-select v-model="model.status" size="mini" style="width:100%;">
              <el-option v-for="item in statusList"
                         :key="item.name" :label="item.name" :value="item.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="要求完成时间" prop="jh_end_time">
            <el-date-picker v-model="model.jh_end_time" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width:100%;"
                            :disabled="!is_creater"></el-date-picker>
          </el-form-item>
        </elm-row>
        <elm-row v-if="edit_type!='create' && is_to_person">
          <el-form-item label="实际完成时间" prop="sj_end_time">
            <el-date-picker v-model="model.sj_end_time" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width:100%;"
                            :disabled="!is_to_person"></el-date-picker>
          </el-form-item>
          <el-form-item></el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" :rows="2" v-model="model.remark"></el-input>
          </el-form-item>
        </elm-row>
      </el-form>

      <template #bottom>
        <el-button type="success" size="small" @click="submit">立即保存</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </template>
    </elm-page-bb>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      loading:true,

      DE_INFO: null,
      edit_type: '',

      model: {},
      task_id: null,

      statusList:[{'name':'未开始'},{'name':'进行中'},{'name':'已完成'},],
      zycdList:[{'name':'低'},{'name':'中'},{'name':'高'},],

      is_creater:false,
      is_to_person:false,
    }
  },
  mounted() {
    this.DE_INFO = elm.dialog.request;

    console.log(this.DE_INFO);
    console.log(this.DE_INFO.flow_code);
    if(this.DE_INFO.model!=null && this.DE_INFO.model.type=='yw_task' && this.DE_INFO.flow_code=='biz_pmis_rwzp'){
      this.edit_type = 'edit';
      this.task_id = this.DE_INFO.model.id;
      this.is_to_person = true;
    }
    else{
      this.edit_type = this.DE_INFO.edit_type;

      if(this.edit_type!='create'){
        this.is_creater = this.DE_INFO.model.is_creater=='yes';
        this.is_to_person = this.DE_INFO.model.is_to_person=='yes';
      }
    }

    console.log('is_creater: '+this.is_creater);
    console.log('is_to_person: '+this.is_to_person);

    this.v_getData();
  },
  methods: {
    async v_getData(){
      this.loading = true;

      if(this.edit_type=='create'){
        this.model = {id: elm.newGuid()};
        this.is_creater = true;
      }
      else{
        if(!this.em(this.task_id)){
          this.model = await elm.post('/business/pmis/rwzp/GetOneByTaskId', {'task_id':this.task_id});
          console.log(JSON.stringify(this.model));
        }
        else{
          this.model = JSON.parse(JSON.stringify(this.DE_INFO.model));
        }
      }

      this.loading = false;
    },

    async submit() {
      try{await this.$refs["model"].validate();}catch{console.log('表单验证未通过'); return;}

      this.loading = true;

      let result = await elm.post('/business/pmis/rwzp/Upsert', this.model);
      if (result == true) {
        this.$message({ type: 'success', message: '保存成功' });
        if(this.DE_INFO.callVue.v_getData!=null){this.DE_INFO.callVue.v_getData();}
        if(!this.em(this.DE_INFO.table_ref)){this.DE_INFO.callVue.tablebinding('task_running');}
        this.v_cancle();
      }
      else { this.$message.error('保存失败，请稍后尝试。'); }

      this.loading = false;
    },
    v_cancle(){
      this.close();//extends: elmVueBase
    },

    em(s) {
      if (s == null || s == '') { return true; }
      else { return false; }
    },

  }
}
</script>
<style>
.one_road .elm-body{padding: 5px;}
</style>